package com.hyg.lib_base.MyView;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;


import com.hyg.lib_base.R;

import java.util.ArrayList;
import java.util.List;

//
///**
// * 自定义雷达图
// * 1、构造函数（初始化）
// * 2、onMeasure（测量大小）
// * 3、onSizeChanged（确定大小）
// * 4、onLayout（子view的位置，如果包含子view的话）
// * 5、onDraw（绘制内容）
// * 6、暴露给外部的接口
// * Created by cjy on 17/8/15.
// */
//
///*       体质辨识的结果 雷达图       */
//public class CustomRadarView extends View {
//    //宽度
//    private int mWidth;
//    //高度
//    private int mHeight;
//    //原点坐标
//    private int centerX;
//    private int centerY;
//    //网格半径
//    private float radius;
//    //网格圈数
//    private int mCount = 10;
//    //几边形
//    private int count = 9;
//    //数据最大值
//    private float maxValue = 100;
//    private float angle ;
//    //Paint
//    private Paint mLinePaint;
//    private Paint mTextPaint;
//    //颜色值
//    private int mLineColor = Color.GRAY;
//    private int mValueColor = Color.BLUE;
//    private int mTextColor = Color.BLACK;
//
//    private Paint circlePaint; //圆点paint
//    private Paint polygonPaint; //边框paint
//    private Paint regionColorPaint; //覆盖区域paint
//
//    //文本集合
////    private List<String> titles;
//    //各维度分值
////    private List<Double> data = new ArrayList<>();
//    private List<BodyConstitutionPoint> Points = new ArrayList<>();
//    private Context context;
//
//    public CustomRadarView(Context context) {
//        super(context);
//        this.context = context;
//        init(context);
//    }
//
//    public CustomRadarView(Context context, @Nullable AttributeSet attrs) {
//        super(context, attrs);
//        this.context = context;
//        init(context);
//
//    }
//    public CustomRadarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
//        super(context, attrs, defStyleAttr);
//        this.context = context;
//        init(context);
//    }
//
//    @Override
//    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//        mWidth = w;
//        mHeight = h;
//        //中心坐标
//        centerX = mWidth/2;
//        centerY = mHeight/2;
//        radius = (Math.min(mWidth,mHeight)/2 * 0.7f);
//        postInvalidate();
//        super.onSizeChanged(w, h, oldw, oldh);
//    }
//
//    private void init(Context context){
//        //计算圆心角
//        angle = ( float) (Math.PI * 2/ count);
//        //        外边框
//        polygonPaint = new Paint();
//        polygonPaint.setColor(ContextCompat.getColor(context, R.color.white));
//        polygonPaint.setAntiAlias( true);
//        polygonPaint.setStyle(Paint.Style.STROKE);
//
//        //        周边文字
//        mTextPaint = new Paint();
//        mTextPaint.setColor(ContextCompat.getColor(context, R.color.black));
//        mTextPaint.setAntiAlias( true);
//        mTextPaint.setStyle(Paint.Style.STROKE);
//        mTextPaint.setTextSize(getResources().getDimension(R.dimen.font12));
//
//
//        //对角线
//        mLinePaint = new Paint();
//        mLinePaint.setStyle(Paint.Style.STROKE);
//        mLinePaint.setAntiAlias( true);
//        mLinePaint.setStrokeWidth(1);
//        mLinePaint.setColor(ContextCompat.getColor(context, R.color.white));
//
//        //坐标圆点
//        circlePaint = new Paint();
//        circlePaint.setColor(ContextCompat.getColor(context, R.color.yellow));
//        circlePaint.setStrokeWidth(1);
//        circlePaint.setAntiAlias( true);
//
//        //选中区域颜色
//        regionColorPaint = new Paint();
//        regionColorPaint.setColor(ContextCompat.getColor(context, R.color.lightcoral));
//        regionColorPaint.setStyle(Paint.Style.FILL);
//        regionColorPaint.setAlpha(175);
//        regionColorPaint.setAntiAlias( true);
//
//        BodyConstitutionPoint item1 = new BodyConstitutionPoint("平和质",0.0);
//        BodyConstitutionPoint item2 = new BodyConstitutionPoint("气虚质",0.0);
//        BodyConstitutionPoint item3 = new BodyConstitutionPoint("湿热质",0.0);
//        BodyConstitutionPoint item4 = new BodyConstitutionPoint("阴虚质",0.0);
//        BodyConstitutionPoint item5 = new BodyConstitutionPoint("气郁质",0.0);
//        BodyConstitutionPoint item6 = new BodyConstitutionPoint("阳虚质",0.0);
//        BodyConstitutionPoint item7 = new BodyConstitutionPoint("痰湿质",0.0);
//        BodyConstitutionPoint item8 = new BodyConstitutionPoint("血瘀质",0.0);
//        BodyConstitutionPoint item9 = new BodyConstitutionPoint("特禀质",0.0);
//        Points.add(item1);
//        Points.add(item2);
//        Points.add(item3);
//        Points.add(item4);
//        Points.add(item5);
//        Points.add(item6);
//        Points.add(item7);
//        Points.add(item8);
//        Points.add(item9);
//
//
//    }
//
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//        //移动原点坐标
//        //        canvas.translate(centerX,centerY);
//        //画网格线
//        drawLine(canvas);
//        //画网格对角线
//        drawGridLine(canvas);
//        //画文本
//        drawText(canvas);
//        //画数据线
//        drawRegion(canvas);
//    }
//
//    /**
//     * 画网格
//     * @param canvas
//     */
//    private void drawLine(Canvas canvas){
//        Path path = new Path();
//        //网格线之间的间距
//        float distance =  radius / (mCount-1);
//        //1度=1*PI/180   360度=2*PI   那么我们每旋转一次的角度为2*PI/内角个数
//        //中心与相邻两个内角相连的夹角角度
//        angle = ( float) (Math.PI * 2/ count);
//
//        for (int i = mCount ; i > 2; i--){//外面的网格图形
//            float currentRadius = i * distance;//当前半径
//            path.reset();
//            //存储最后一圈网格的点的坐标
//            for(int j = 0 ; j < count; j++){
//                if(j == 0 ){
//                    path.moveTo(centerX, centerY -currentRadius);
//                }else{
//                    //对于直角三角形sin(x)是对边比斜边，cos(x)是底边比斜边，tan(x)是对边比底边
//                    //因此可以推导出:底边(x坐标)=斜边(半径)*cos(夹角角度)
//                    //               对边(y坐标)=斜边(半径)*sin(夹角角度)
//                    float x = ( float) (centerX + Math. sin(angle * j) * currentRadius);
//                    float y = ( float) (centerY - Math. cos(angle * j) * currentRadius);
//                    path.lineTo(x,y);
//                }
//            }
//            path.close();
//            //区域上色
//            if(i == 3 ){
//                polygonPaint.setColor(ContextCompat.getColor(context, R.color.InnerRingPurples));
//                polygonPaint.setStyle(Paint.Style.FILL);
//                polygonPaint.setAntiAlias( true);
//            }
//            else if(i == 4 ){
//                polygonPaint.setColor(ContextCompat.getColor(context, R.color.InnerRingPurple));
//                polygonPaint.setStyle(Paint.Style.FILL);
//                polygonPaint.setAntiAlias( true);
//            }
//            else if(i == 6){
//                polygonPaint.setColor(ContextCompat.getColor(context, R.color.MiddleRingPurple));
//                polygonPaint.setStyle(Paint.Style.FILL);
//                polygonPaint.setAntiAlias( true);
//                i--;
//            }else if(i == 9){
//                polygonPaint.setColor(ContextCompat.getColor(context, R.color.OuterRingPurple));
//                polygonPaint.setStyle(Paint.Style.FILL);
//                polygonPaint.setAntiAlias( true);
//                i-=2;
//            }else if(i == 10){
//                polygonPaint.setColor(ContextCompat.getColor(context, R.color.transparent));
//                polygonPaint.setStyle(Paint.Style.FILL);
//                polygonPaint.setAntiAlias( true);
//            }
//            canvas.drawPath(path, polygonPaint);
//
//            polygonPaint.setColor(ContextCompat.getColor(context, R.color.white));
//            polygonPaint.setAntiAlias(true);
//            polygonPaint.setStyle(Paint.Style.STROKE);
//            polygonPaint.setAlpha(255);
//            canvas.drawPath(path, polygonPaint);
//
//        }
//    }
//
//    /**
//     * 画网格对角线
//     * @param canvas
//     */
//    private void drawGridLine(Canvas canvas){
//        Path path = new Path();
//        //        float r = radius / count;
//        for( int i = 0; i < count; i++) {
//            //起始坐标 从中心开始的话 startx=centerX , startY=centerY
//            ////            float startX = ( float) (centerX + Math. sin(angle * i) * r);
//            ////            float startY = ( float) (centerY - Math. cos(angle * i) * r);
//            float startX = ( float) (centerX);
//            float startY = ( float) (centerY);
//            //末端坐标
//            float endX = ( float) (centerX + Math. sin(angle * i) * radius);
//            float endY = ( float) (centerY - Math. cos(angle * i) * radius);
//            path.reset();
//            path.moveTo(startX, startY);
//            path.lineTo(endX, endY);
//            canvas.drawPath(path, mLinePaint);
//        }
//    }
//
//    /**
//     * 画文本
//     * @param canvas
//     */
//    private void drawText(Canvas canvas){
//        if (count != Points.size()) {
//            return;
//        }
//        double maxvalue = 0.0;
//        int maxpoint = 0;
//
//        for(int k = 1; k < Points.size(); k++){
//            if(Points.get(k).getConversionScore() >= maxvalue){
//                if(k != 0 ) {
//                    maxvalue = Points.get(k).getConversionScore();
//                    maxpoint = k;
//                }
//            }
//        }
//
////        if(!Points.get(0).getBodyConstitutionTypes().equals("平和质") ) {
//        if(maxvalue >= Points.get(0).getConversionScore()){
////            maxvalue = Points.get(0).getConversionScore();
//            maxpoint = 0;
//        }else{
////            maxvalue = Points.get(1).getConversionScore();
//            if(Points.get(0).getConversionScore() >= 60 )
//                maxpoint = 1;
//            else{
//                maxpoint = 2;
//            }
//        }
//
//        for (int i = 0; i < count; i++){
//            double Pointdatas = Points.get(i).getConversionScore();
//            Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
//            Typeface font1 = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
//            String name = Points.get(i).getBodyConstitutionTypes();
//            if(name.equals("平和质")){
//                if(maxvalue < 40 && Pointdatas >=60 ){
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.SelectedWord));
//                    mTextPaint.setAlpha(255);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font16));
//                    mTextPaint.setTypeface(font);
//                }else {
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.text_deep));
//                    mTextPaint.setAlpha(150);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font14));
//                    mTextPaint.setTypeface(font1);
//                }
//            }else{
//                if(maxvalue >= 40 && Pointdatas >= 40){
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.SelectedWord));
//                    mTextPaint.setAlpha(255);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font16));
//                    mTextPaint.setTypeface(font);
//                } else if (maxvalue >= 40 && Pointdatas < 40 && Pointdatas >= 30) {
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.logoPink));
//                    mTextPaint.setAlpha(200);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font15));
//                    mTextPaint.setTypeface(font);
//                }else if(maxvalue < 40 && Pointdatas >= 30 && maxpoint == 2){
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.logoPink));
//                    mTextPaint.setAlpha(200);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font15));
//                    mTextPaint.setTypeface(font);
//                }else if(maxvalue < 40 && Pointdatas >= 30 && maxpoint == 1){
//                    mTextPaint.setTypeface( font1 );
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.logoPink));
//                    mTextPaint.setAlpha(200);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font15));
//                }else {
//                    mTextPaint.setColor(ContextCompat.getColor(context, R.color.text_deep));
//                    mTextPaint.setAlpha(150);
//                    mTextPaint.setTextSize(getResources().getDimension(R.dimen.font14));
//                    mTextPaint.setTypeface(font1);
//                }
//            }
//
//            String txt = Points.get(i).getBodyConstitutionTypes();
//            Rect bounds = new Rect();
//            float height = bounds.bottom - bounds.top;
//            float width = mTextPaint.measureText(txt);
//
//            Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
//            float fontHeight = fontMetrics.descent - fontMetrics.ascent;
//
//            float x = ( float) (centerX + Math. sin(angle * i) * (radius + fontHeight/2));
//            float y = (float) (centerY - Math.cos(angle * i) * (radius + fontHeight/2.5));
//
//            if(angle * i == 0) {
//                //第一个文字位于顶角正上方
//                mTextPaint.setTextAlign(Paint.Align.CENTER);
//                canvas.drawText(txt, x, y , mTextPaint);
//            } else if(angle * i > 0 && angle * i < Math.PI / 2) {//第一象限
//                //微调
//                float dis = mTextPaint.measureText(txt);//文本长度
//                canvas.drawText(txt, x + dis/3, y + fontHeight/4 , mTextPaint);
//
//            } else if(angle * i >= Math.PI / 2 && angle * i < Math.PI) {//第四象限
//                //最右下的文字获取到文字的长、宽，按文字长度百分比向左移
//                float dis = mTextPaint.measureText(txt);//文本长度
//                canvas.drawText(txt, x + dis/4, y + fontHeight/2, mTextPaint);
//
//            } else if(angle * i >= Math.PI && angle * i < 3* Math.PI / 2) {//第三象限
//                //同理最左下的文字获取到文字的长、宽，按文字长度百分比向左移
//                float dis = mTextPaint.measureText(txt);//文本长度
//                canvas.drawText(txt, x - dis/4, y + fontHeight/2, mTextPaint);
//            } else if(angle * i >= 3* Math.PI / 2 && angle * i < 2* Math.PI) {//第二象限
//                //文字向左移动
//                float dis = mTextPaint.measureText(txt);//文本长度
//                canvas.drawText(txt, x - dis/3, y + fontHeight/4 , mTextPaint);
//            }
//
//        }
//    }
//
//    private void drawRegion(Canvas canvas){
//        Path path = new Path();
//        float r = radius / count; //每层的间距
//        double maxvalue =0.0;
//        int maxpoint =  0;
//
//        for(int k = 1; k < Points.size(); k++){
//            if(Points.get(k).getConversionScore() >= maxvalue){
//                if(k != 0 ) {
//                    maxvalue = Points.get(k).getConversionScore();
//                    maxpoint = k;
//                }
//            }
//        }
//
//        //        if(!Points.get(0).getBodyConstitutionTypes().equals("平和质") ) {
//        if(maxvalue >= Points.get(0).getConversionScore()){
//            //            maxvalue = Points.get(0).getConversionScore();
//            maxpoint = 0;
//        }else{
//            //            maxvalue = Points.get(1).getConversionScore();
//            if(Points.get(0).getConversionScore() >= 60 )
//                maxpoint = 1;
//            else{
//                maxpoint = 2;
//            }
//        }
//
//        for( int i = 0; i < count; i++) {
//            //计算该数值与最大值比例
//            Double perCenter = Points.get(i).getConversionScore() / maxValue;
//            //小圆点所在位置距离圆心的距离
//            double perRadius = perCenter * radius;
//            float x = (float) (centerX + Math.sin(angle * i) * (perCenter * (radius - r) + r));
//            float y = (float) (centerY - Math.cos(angle * i) * (perCenter * (radius - r) + r));
//            if (i == 0) {
//                path.moveTo(x, y);
//            } else {
//                path.lineTo(x, y);
//            }
//        }
//        path.close();
//        canvas.drawPath(path, regionColorPaint);
//        path = new Path();
//        for( int i = 0; i < count; i++) {
//            //计算该数值与最大值比例
//            Double perCenter = Points.get(i).getConversionScore() / maxValue;
//            //小圆点所在位置距离圆心的距离
//            double perRadius = perCenter * radius;
//            float x = (float) (centerX + Math.sin(angle * i) * (perCenter * (radius - r) + r));
//            float y = (float) (centerY - Math.cos(angle * i) * (perCenter * (radius - r) + r));
//
//            double Pointdatas = Points.get(i).getConversionScore();
//            String name = Points.get(i).getBodyConstitutionTypes();
//            if(name.equals("平和质")){
//                if(maxvalue < 40 && Pointdatas >=60 ){
//                    circlePaint.setAlpha(180);
//                    circlePaint.setColor(ContextCompat.getColor(context, R.color.topcolor));
//                    canvas.drawCircle(x,y,getResources().getDimension(R.dimen.font5),circlePaint);
//                }else{
//                    circlePaint.setColor(ContextCompat.getColor(context, R.color.SelectedBall));
//                    circlePaint.setAlpha(200);
//                    canvas.drawCircle(x,y,getResources().getDimension(R.dimen.font4),circlePaint);
//                }
//            }else{
//                if(maxvalue >= 40 && Pointdatas >= 40){
//                    circlePaint.setAlpha(255);
//                    circlePaint.setColor(ContextCompat.getColor(context, R.color.topcolor));
//                    canvas.drawCircle(x,y,getResources().getDimension(R.dimen.font5),circlePaint);
//                }else if(maxvalue < 40 && Pointdatas >= 30 && maxpoint == 2){
//                    circlePaint.setAlpha(255);
//                    circlePaint.setColor(ContextCompat.getColor(context, R.color.topcolor));
//                    canvas.drawCircle(x,y,getResources().getDimension(R.dimen.font5),circlePaint);
//                }else if(maxvalue < 40 && Pointdatas >= 30 && maxpoint == 1){
//                    circlePaint.setAlpha(200);
//                    circlePaint.setColor(ContextCompat.getColor(context, R.color.topcolor));
//                    canvas.drawCircle(x,y,getResources().getDimension(R.dimen.font4),circlePaint);
//                }else{
//                    circlePaint.setColor(ContextCompat.getColor(context, R.color.SelectedBall));
//                    circlePaint.setAlpha(200);
//                    canvas.drawCircle(x,y,getResources().getDimension(R.dimen.font4),circlePaint);
//                }
//            }
//        }
//    }
//
//
//    /**
//     * 设置边数
//     * @param count
//     */
//    public void setCount(int count){
//        this.count = count;
//        angle = ( float) (Math.PI * 2/ count);
//        postInvalidate();
//    }
//    //设置圈数
//    public void setLayerCount(int layerCount){
//        this.count = layerCount;
//        postInvalidate();
//    }
//    /**
//     * 设置网格线颜色
//     * @param color
//     */
//    public void setLineColor(int color){
//        mLineColor = color;
//        mLinePaint.setColor(mLineColor);
//        postInvalidate();
//    }
//
//    /**
//     * 设置文本颜色
//     * @param color
//     */
//    public void setTextColor(int color){
//        mTextColor = color;
//        mTextPaint.setColor(mTextColor);
//        postInvalidate();
//    }
//
//    //设置蜘蛛网颜色
//    public void setMainPaint(Paint mainPaint) {
//        this.polygonPaint = mainPaint;
//        postInvalidate();
//    }
//
//    //设置标题颜色
//    public void setTextPaint(Paint textPaint) {
//        this.mTextPaint = textPaint;
//    }
//
//
//    //设置覆盖局域颜色
//    public void setValuePaint(Paint valuePaint) {
//        this.regionColorPaint = valuePaint;
//        postInvalidate();
//    }
//
//    //设置数值
//    public void setData(List<BodyConstitutionPoint> ps) {
//        for(int i =0 ; i < ps.size(); i++){
//            for(int j = 0; j <ps.size(); j++){
//                 if(Points.get(j).getBodyConstitutionTypes().equals(ps.get(i).getBodyConstitutionTypes()))    {
//                     Points.get(j).setConversionScore( ps.get(i).getConversionScore() );
//                 }
//            }
//        }
//
//        postInvalidate();
//    }
//
//
//    //设置最大数值
//    public void setMaxValue(float maxValue) {
//        this.maxValue = maxValue;
//    }
//
//
//}






/**
 * 自定义雷达图
 * 1、构造函数（初始化）
 * 2、onMeasure（测量大小）
 * 3、onSizeChanged（确定大小）
 * 4、onLayout（子view的位置，如果包含子view的话）
 * 5、onDraw（绘制内容）
 * 6、暴露给外部的接口
 * Created by cjy on 17/8/15.
 */

/*       体质辨识的结果 雷达图       */
public class CustomRadarView extends View {
    //宽度
    private int mWidth;
    //高度
    private int mHeight;
    //原点坐标
    private int centerX;
    private int centerY;
    //网格半径
    private float radius;
    //网格圈数
    private int mCount = 10;
    //几边形
    private int count = 9;
    //数据最大值
    private float maxValue = 100;
    private float angle;
    //Paint
    private Paint mLinePaint;
    private Paint mTextPaint;
    //颜色值
    private int mLineColor = Color.GRAY;
    private int mValueColor = Color.BLUE;
    private int mTextColor = Color.BLACK;

    private Paint circlePaint; //圆点paint
    private Paint polygonPaint; //边框paint
    private Paint regionColorPaint; //覆盖区域paint

    String MainBody = "", secondaryBody = "";


    //文本集合
//    private List<String> titles;
    //各维度分值
//    private List<Double> data = new ArrayList<>();
    private List<BodyConstitutionPoint> Points = new ArrayList<>();
    private Context context;

    public CustomRadarView(Context context) {
        super(context);
        this.context = context;
        init(context);
    }

    public CustomRadarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(context);

    }

    public CustomRadarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mWidth = w;
        mHeight = h;
        //中心坐标
        centerX = mWidth / 2;
        centerY = mHeight / 2;
        radius = (Math.min(mWidth, mHeight) / 2 * 0.7f);
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void init(Context context) {
        //计算圆心角
        angle = (float) (Math.PI * 2 / count);
        //        外边框
        polygonPaint = new Paint();
        polygonPaint.setColor(ContextCompat.getColor(context, R.color.white));
        polygonPaint.setAntiAlias(true);
        polygonPaint.setStyle(Paint.Style.STROKE);

        //        周边文字
        mTextPaint = new Paint();
        mTextPaint.setColor(ContextCompat.getColor(context, R.color.black));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(getResources().getDimension(R.dimen.font12));


        //对角线
        mLinePaint = new Paint();
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStrokeWidth(1);
        mLinePaint.setColor(ContextCompat.getColor(context, R.color.white));

        //坐标圆点
        circlePaint = new Paint();
        circlePaint.setColor(ContextCompat.getColor(context, R.color.yellow));
        circlePaint.setStrokeWidth(1);
        circlePaint.setAntiAlias(true);

        //选中区域颜色
        regionColorPaint = new Paint();
        regionColorPaint.setColor(ContextCompat.getColor(context, R.color.lightcoral));
        regionColorPaint.setStyle(Paint.Style.FILL);
        regionColorPaint.setAlpha(175);
        regionColorPaint.setAntiAlias(true);

        BodyConstitutionPoint item1 = new BodyConstitutionPoint("平和质", 0.0);
        BodyConstitutionPoint item2 = new BodyConstitutionPoint("气虚质", 0.0);
        BodyConstitutionPoint item3 = new BodyConstitutionPoint("湿热质", 0.0);
        BodyConstitutionPoint item4 = new BodyConstitutionPoint("阴虚质", 0.0);
        BodyConstitutionPoint item5 = new BodyConstitutionPoint("气郁质", 0.0);
        BodyConstitutionPoint item6 = new BodyConstitutionPoint("阳虚质", 0.0);
        BodyConstitutionPoint item7 = new BodyConstitutionPoint("痰湿质", 0.0);
        BodyConstitutionPoint item8 = new BodyConstitutionPoint("血瘀质", 0.0);
        BodyConstitutionPoint item9 = new BodyConstitutionPoint("特禀质", 0.0);
        Points.add(item1);
        Points.add(item2);
        Points.add(item3);
        Points.add(item4);
        Points.add(item5);
        Points.add(item6);
        Points.add(item7);
        Points.add(item8);
        Points.add(item9);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //移动原点坐标
        //        canvas.translate(centerX,centerY);
        //画网格线
        drawLine(canvas);
        //画网格对角线
        drawGridLine(canvas);
        //画文本
        drawText(canvas);
        //画数据线
        drawRegion(canvas);
    }

    /**
     * 画网格
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        Path path = new Path();
        //网格线之间的间距
        float distance = radius / (mCount - 1);
        //1度=1*PI/180   360度=2*PI   那么我们每旋转一次的角度为2*PI/内角个数
        //中心与相邻两个内角相连的夹角角度
        angle = (float) (Math.PI * 2 / count);

        for (int i = mCount; i > 2; i--) {//外面的网格图形
            float currentRadius = i * distance;//当前半径
            path.reset();
            //存储最后一圈网格的点的坐标
            for (int j = 0; j < count; j++) {
                if (j == 0) {
                    path.moveTo(centerX, centerY - currentRadius);
                } else {
                    //对于直角三角形sin(x)是对边比斜边，cos(x)是底边比斜边，tan(x)是对边比底边
                    //因此可以推导出:底边(x坐标)=斜边(半径)*cos(夹角角度)
                    //               对边(y坐标)=斜边(半径)*sin(夹角角度)
                    float x = (float) (centerX + Math.sin(angle * j) * currentRadius);
                    float y = (float) (centerY - Math.cos(angle * j) * currentRadius);
                    path.lineTo(x, y);
                }
            }
            path.close();
            //区域上色
            if (i == 3) {
                polygonPaint.setColor(ContextCompat.getColor(context, R.color.InnerRingPurples));
                polygonPaint.setStyle(Paint.Style.FILL);
                polygonPaint.setAntiAlias(true);
            } else if (i == 4) {
                polygonPaint.setColor(ContextCompat.getColor(context, R.color.InnerRingPurple));
                polygonPaint.setStyle(Paint.Style.FILL);
                polygonPaint.setAntiAlias(true);
            } else if (i == 6) {
                polygonPaint.setColor(ContextCompat.getColor(context, R.color.MiddleRingPurple));
                polygonPaint.setStyle(Paint.Style.FILL);
                polygonPaint.setAntiAlias(true);
                i--;
            } else if (i == 9) {
                polygonPaint.setColor(ContextCompat.getColor(context, R.color.OuterRingPurple));
                polygonPaint.setStyle(Paint.Style.FILL);
                polygonPaint.setAntiAlias(true);
                i -= 2;
            } else if (i == 10) {
                polygonPaint.setColor(ContextCompat.getColor(context, R.color.transparent));
                polygonPaint.setStyle(Paint.Style.FILL);
                polygonPaint.setAntiAlias(true);
            }
            canvas.drawPath(path, polygonPaint);

            polygonPaint.setColor(ContextCompat.getColor(context, R.color.white));
            polygonPaint.setAntiAlias(true);
            polygonPaint.setStyle(Paint.Style.STROKE);
            polygonPaint.setAlpha(255);
            canvas.drawPath(path, polygonPaint);

        }
    }

    /**
     * 画网格对角线
     *
     * @param canvas
     */
    private void drawGridLine(Canvas canvas) {
        Path path = new Path();
        //        float r = radius / count;
        for (int i = 0; i < count; i++) {
            //起始坐标 从中心开始的话 startx=centerX , startY=centerY
            ////            float startX = ( float) (centerX + Math. sin(angle * i) * r);
            ////            float startY = ( float) (centerY - Math. cos(angle * i) * r);
            float startX = (float) (centerX);
            float startY = (float) (centerY);
            //末端坐标
            float endX = (float) (centerX + Math.sin(angle * i) * radius);
            float endY = (float) (centerY - Math.cos(angle * i) * radius);
            path.reset();
            path.moveTo(startX, startY);
            path.lineTo(endX, endY);
            canvas.drawPath(path, mLinePaint);
        }
    }

    /**
     * 画文本
     *
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        if (count != Points.size()) {
            return;
        }
        double maxvalue = 0.0;
        int maxpoint = 0;

        for (int k = 1; k < Points.size(); k++) {
            if (Points.get(k).getConversionScore() >= maxvalue) {
                if (k != 0) {
                    maxvalue = Points.get(k).getConversionScore();
                    maxpoint = k;
                }
            }
        }

//        if(!Points.get(0).getBodyConstitutionTypes().equals("平和质") ) {
        if (maxvalue >= Points.get(0).getConversionScore()) {
//            maxvalue = Points.get(0).getConversionScore();
            maxpoint = 0;
        } else {
//            maxvalue = Points.get(1).getConversionScore();
            if (Points.get(0).getConversionScore() >= 60)
                maxpoint = 1;
            else {
                maxpoint = 2;
            }
        }

        for (int i = 0; i < count; i++) {
            String name1 = Points.get(i).getBodyConstitutionTypes();
            Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
            Typeface font1 = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL);
            if (name1.equals(MainBody) || name1.equals(secondaryBody)) {
                mTextPaint.setColor(ContextCompat.getColor(context, R.color.SelectedWord));
                mTextPaint.setAlpha(255);
                mTextPaint.setTextSize(getResources().getDimension(R.dimen.font16));
                mTextPaint.setTypeface(font);
            } else {
                mTextPaint.setColor(ContextCompat.getColor(context, R.color.text_deep));
                mTextPaint.setAlpha(150);
                mTextPaint.setTextSize(getResources().getDimension(R.dimen.font14));
                mTextPaint.setTypeface(font1);
            }

            String txt = Points.get(i).getBodyConstitutionTypes();
            Rect bounds = new Rect();
            float height = bounds.bottom - bounds.top;
            float width = mTextPaint.measureText(txt);

            Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
            float fontHeight = fontMetrics.descent - fontMetrics.ascent;

            float x = (float) (centerX + Math.sin(angle * i) * (radius + fontHeight / 2));
            float y = (float) (centerY - Math.cos(angle * i) * (radius + fontHeight / 2.5));

            if (angle * i == 0) {
                //第一个文字位于顶角正上方
                mTextPaint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(txt, x, y, mTextPaint);
            } else if (angle * i > 0 && angle * i < Math.PI / 2) {//第一象限
                //微调
                float dis = mTextPaint.measureText(txt);//文本长度
                canvas.drawText(txt, x + dis / 3, y + fontHeight / 4, mTextPaint);

            } else if (angle * i >= Math.PI / 2 && angle * i < Math.PI) {//第四象限
                //最右下的文字获取到文字的长、宽，按文字长度百分比向左移
                float dis = mTextPaint.measureText(txt);//文本长度
                canvas.drawText(txt, x + dis / 4, y + fontHeight / 2, mTextPaint);

            } else if (angle * i >= Math.PI && angle * i < 3 * Math.PI / 2) {//第三象限
                //同理最左下的文字获取到文字的长、宽，按文字长度百分比向左移
                float dis = mTextPaint.measureText(txt);//文本长度
                canvas.drawText(txt, x - dis / 4, y + fontHeight / 2, mTextPaint);
            } else if (angle * i >= 3 * Math.PI / 2 && angle * i < 2 * Math.PI) {//第二象限
                //文字向左移动
                float dis = mTextPaint.measureText(txt);//文本长度
                canvas.drawText(txt, x - dis / 3, y + fontHeight / 4, mTextPaint);
            }

        }
    }

    private void drawRegion(Canvas canvas) {
        Path path = new Path();
        float r = radius / count; //每层的间距
        double maxvalue = 0.0;
        int maxpoint = 0;

        for (int k = 1; k < Points.size(); k++) {
            if (Points.get(k).getConversionScore() >= maxvalue) {
                if (k != 0) {
                    maxvalue = Points.get(k).getConversionScore();
                    maxpoint = k;
                }
            }
        }

        //        if(!Points.get(0).getBodyConstitutionTypes().equals("平和质") ) {
        if (maxvalue >= Points.get(0).getConversionScore()) {
            //            maxvalue = Points.get(0).getConversionScore();
            maxpoint = 0;
        } else {
            //            maxvalue = Points.get(1).getConversionScore();
            if (Points.get(0).getConversionScore() >= 60)
                maxpoint = 1;
            else {
                maxpoint = 2;
            }
        }

        for (int i = 0; i < count; i++) {
            //计算该数值与最大值比例
            Double perCenter = Points.get(i).getConversionScore() / maxValue;
            //小圆点所在位置距离圆心的距离
            double perRadius = perCenter * radius;
            float x = (float) (centerX + Math.sin(angle * i) * (perCenter * (radius - r) + r));
            float y = (float) (centerY - Math.cos(angle * i) * (perCenter * (radius - r) + r));
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }
        }
        path.close();
        canvas.drawPath(path, regionColorPaint);
        path = new Path();
        for (int i = 0; i < count; i++) {
            //计算该数值与最大值比例
            Double perCenter = Points.get(i).getConversionScore() / maxValue;
            //小圆点所在位置距离圆心的距离
            double perRadius = perCenter * radius;
            float x = (float) (centerX + Math.sin(angle * i) * (perCenter * (radius - r) + r));
            float y = (float) (centerY - Math.cos(angle * i) * (perCenter * (radius - r) + r));

            double Pointdatas = Points.get(i).getConversionScore();
            String name = Points.get(i).getBodyConstitutionTypes();

            if (name.equals(MainBody) || name.equals(secondaryBody)) {
                circlePaint.setAlpha(180);
                circlePaint.setColor(ContextCompat.getColor(context, R.color.topcolor));
                canvas.drawCircle(x, y, getResources().getDimension(R.dimen.font5), circlePaint);
            } else {
                circlePaint.setColor(ContextCompat.getColor(context, R.color.SelectedBall));
                circlePaint.setAlpha(200);
                canvas.drawCircle(x, y, getResources().getDimension(R.dimen.font4), circlePaint);
            }

        }

    }


    public void setBody(String mainBody, String secondaryBody) {
        this.MainBody = mainBody;
        this.secondaryBody = secondaryBody;
    }


    /**
     * 设置边数
     *
     * @param count
     */
    public void setCount(int count) {
        this.count = count;
        angle = (float) (Math.PI * 2 / count);
        postInvalidate();
    }

    //设置圈数
    public void setLayerCount(int layerCount) {
        this.count = layerCount;
        postInvalidate();
    }

    /**
     * 设置网格线颜色
     *
     * @param color
     */
    public void setLineColor(int color) {
        mLineColor = color;
        mLinePaint.setColor(mLineColor);
        postInvalidate();
    }

    /**
     * 设置文本颜色
     *
     * @param color
     */
    public void setTextColor(int color) {
        mTextColor = color;
        mTextPaint.setColor(mTextColor);
        postInvalidate();
    }

    //设置蜘蛛网颜色
    public void setMainPaint(Paint mainPaint) {
        this.polygonPaint = mainPaint;
        postInvalidate();
    }

    //设置标题颜色
    public void setTextPaint(Paint textPaint) {
        this.mTextPaint = textPaint;
    }


    //设置覆盖局域颜色
    public void setValuePaint(Paint valuePaint) {
        this.regionColorPaint = valuePaint;
        postInvalidate();
    }

    //设置数值
    public void setData(List<BodyConstitutionPoint> ps) {
        for (int i = 0; i < ps.size(); i++) {
            for (int j = 0; j < ps.size(); j++) {
                if (Points.get(j).getBodyConstitutionTypes().equals(ps.get(i).getBodyConstitutionTypes())) {
                    Points.get(j).setConversionScore(ps.get(i).getConversionScore());
                }
            }
        }
        postInvalidate();
    }


    //设置最大数值
    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

}
